<?php

namespace App\Admin\Controllers;

use App\Admin\Models\Goods;
use App\Admin\Models\Group;
use App\Admin\Models\Material;
use App\Admin\Models\Price;
use App\Admin\Models\Shape;
use App\Admin\Models\Shop;
use App\Admin\Models\Style;
use App\Admin\Models\Type;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class GoodsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('商品')
            ->description(trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('商品')
            ->description(trans('admin.view'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('商品')
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('商品')
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Goods);

        $grid->id('ID')->sortable();
        $grid->name('名称');
        $grid->picture('图片')->image();
        $grid->price('价格');
        $grid->sale('销售状态')->radio(Goods::getSales());
        $grid->column('type.name', '系列');
        $grid->column('style.name', '风格');
        $grid->column('material.name', '材质');
        $grid->column('shape.name', '框形');
        $grid->column('group.name', '人群');
    
        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('shop_name', '名称');
            $filter->between('price', '价格');
            $filter->equal('type_id', '系列')->select(Type::getMaps('id', 'name'));
            $filter->equal('style_id', '风格')->select(Style::getMaps('id', 'name'));
            $filter->equal('material_id', '材质')->select(Material::getMaps('id', 'name'));
            $filter->equal('shape_id', '框形')->select(Shape::getMaps('id', 'name'));
            $filter->equal('group_id', '人群')->select(Group::getMaps('id', 'name'));
            $filter->where(function ($query) {
                $price = $this->input;
                if ($price == '其它') {
                    $query->whereNotIn('price', Price::getNumberPrice());
                } else {
                    $query->where('price', $price);
                }
            }, '价格')->select(Price::getMaps('name', 'name'));
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Goods::findOrFail($id));

        $show->id('ID');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Goods);

        $form->display('id', 'ID');
        $form->text('name', '名称')->rules('required');
        $form->currency('price', '价格')->symbol('￥')->rules('required');
        $form->radio('sale', '销售状态')->options(Goods::getSales())->default(0);
        $form->text('link', '外链');
        $form->textarea('brief', '简介');
        $form->select('shop_id', '店铺')->options(Shop::getMaps('id', 'name'))->rules('required');
        $form->select('type_id', '系列')->options(Type::getMaps('id', 'name'))->rules('required');
        $form->select('style_id', '风格')->options(Style::getMaps('id', 'name'))->rules('required');
        $form->select('material_id', '材质')->options(Material::getMaps('id', 'name'))->rules('required');
        $form->select('shape_id', '框形')->options(Shape::getMaps('id', 'name'))->rules('required');
        $form->select('group_id', '人群')->options(Group::getMaps('id', 'name'))->rules('required');
        $form->image('picture', '图片');
        $form->display('created_at', '创建时间');
        $form->display('updated_at', '修改时间');

        return $form;
    }
}
